package com.tledu.schedulingInfo.service.impl;

import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.common.utils.StringUtils;
import com.tledu.schedulingInfo.domain.SchedulingInfo;
import com.tledu.schedulingInfo.domain.SchedulingItem;
import com.tledu.schedulingInfo.domain.dto.SchedulingInfoDTO;
import com.tledu.schedulingInfo.domain.dto.SchedulingItemDTO;
import com.tledu.schedulingInfo.domain.vo.SchedulingItemVO;
import com.tledu.schedulingInfo.mapper.SchedulingInfoMapper;
import com.tledu.schedulingInfo.mapper.SchedulingItemMapper;
import com.tledu.schedulingInfo.service.ISchedulingInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 排班Service业务层处理
 *
 * @author longxf
 * @date 2024-09-06
 */
@Service
public class SchedulingInfoServiceImpl implements ISchedulingInfoService {
    @Resource
    private SchedulingInfoMapper schedulingInfoMapper;
    @Resource
    private SchedulingItemMapper schedulingItemMapper;

    /**
     * 查询排班
     *
     * @param id 排班主键
     * @return 排班
     */
    @Override
    public SchedulingInfoDTO selectSchedulingInfoById(Long id) {
        //创建SchedulingInfoDTO对象
        SchedulingInfoDTO schedulingInfoDTO = new SchedulingInfoDTO();
        //将schedulingInfo拷贝给SchedulingInfoDTO对象
        SchedulingInfo schedulingInfo = schedulingInfoMapper.selectSchedulingInfoById(id);
        BeanUtils.copyProperties(schedulingInfo, schedulingInfoDTO);

        List<SchedulingItemVO> schedulingItemVOList = schedulingItemMapper.getSchedulingItemVO(id);

        //定义空的SchedulingItemDTO的list集合
        List<SchedulingItemDTO> schedulingItemDTOList = new ArrayList<>();

        for (SchedulingItemVO schedulingItemVO : schedulingItemVOList) {

            SchedulingItemDTO schedulingItemDTO = new SchedulingItemDTO();
            //设置值班日期
            schedulingItemDTO.setSchedulingDate(schedulingItemVO.getSchedulingDate());



            List<Long> schedulingUserIds = Arrays.stream(schedulingItemVO.getSchedulingUserIds().split(","))
                    .map(userId -> Long.parseLong(userId)).collect(Collectors.toList());

            schedulingItemDTO.setSchedulingUserIds(schedulingUserIds);
            schedulingItemDTOList.add(schedulingItemDTO);
        }

        //为schedulingInfoDTO对象设置schedulingItemDTOList
        schedulingInfoDTO.setSchedulingItemList(schedulingItemDTOList);
        //返回SchedulingInfoDTO对象
        return schedulingInfoDTO;
    }

    /**
     * 查询排班列表
     *
     * @param schedulingInfo 排班
     * @return 排班
     */
    @Override
    public List<SchedulingInfo> selectSchedulingInfoList(SchedulingInfo schedulingInfo) {
        return schedulingInfoMapper.selectSchedulingInfoList(schedulingInfo);
    }

    /**
     * 新增排班
     *
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSchedulingInfo(SchedulingInfoDTO schedulingInfoDTO) {
        SchedulingInfo schedulingInfo = new SchedulingInfo();
        //BeanUtils这是spring提供的bean的工具类 copyProperties 拷贝属性
        //source和target 属性名要一样类型要能转赚
        BeanUtils.copyProperties(schedulingInfoDTO, schedulingInfo);
        //完善主表信息
        schedulingInfo.setCreateTime(DateUtils.getNowDate());
        schedulingInfo.setCreateUser(SecurityUtils.getUserId());
        //插入主表记录
        int rows = schedulingInfoMapper.insertSchedulingInfo(schedulingInfo);
        //插入明细表
        insertSchedulingItem(schedulingInfoDTO.getSchedulingItemList(), schedulingInfo.getId());
        return rows;
    }

    /**
     * 修改排班
     *
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSchedulingInfo(SchedulingInfoDTO schedulingInfoDTO) {
        schedulingInfoMapper.deleteSchedulingItemByScheduleId(schedulingInfoDTO.getId());
        insertSchedulingItem(schedulingInfoDTO.getSchedulingItemList(), schedulingInfoDTO.getId());
        SchedulingInfo schedulingInfo = new SchedulingInfo();
        BeanUtils.copyProperties(schedulingInfoDTO, schedulingInfo);
        return schedulingInfoMapper.updateSchedulingInfo(schedulingInfo);
    }

    /**
     * 批量删除排班
     *
     * @param ids 需要删除的排班主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSchedulingInfoByIds(Long[] ids) {
        // schedulingInfoMapper.deleteSchedulingItemBySchedulingIds(ids);
        return schedulingInfoMapper.deleteSchedulingInfoByIds(ids);
    }

    /**
     * 删除排班信息
     *
     * @param id 排班主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSchedulingInfoById(Long id) {
        // schedulingInfoMapper.deleteSchedulingItemBySchedulingId(id);
        return schedulingInfoMapper.deleteSchedulingInfoById(id);
    }

    /**
     * 新增排班明细信息
     */
    public void insertSchedulingItem(List<SchedulingItemDTO> schedulingItemList, Long schedulingInfoId) {
        if (StringUtils.isNotNull(schedulingItemList)) {
            List<SchedulingItem> list = new ArrayList<SchedulingItem>();
            //遍历schedulingItemList获取SchedulingItemDTO对象
            for (SchedulingItemDTO schedulingItemDTO : schedulingItemList) {
                //遍历SchedulingItemDTO中的ids,为每一个id创建一个schedulingItem对象
                List<Long> schedulingUserIds = schedulingItemDTO.getSchedulingUserIds();
                for (Long schedulingUserId : schedulingUserIds) {
                    SchedulingItem schedulingItem = new SchedulingItem();
                    //设置排班主表id
                    // schedulingItem.setSchedulingId(schedulingInfoId);
                    schedulingItem.setScheduleId(schedulingInfoId);
                    //设置值班人id
                    schedulingItem.setSchedulingUserId(schedulingUserId);
                    //设置值班日期
                    schedulingItem.setSchedulingDate(schedulingItemDTO.getSchedulingDate());
                    list.add(schedulingItem);
                }
            }
            //批量插入schedulingItem表
            if (list.size() > 0) {
                schedulingInfoMapper.batchSchedulingItem(list);
            }
        }
    }
}
